package cn.valo.system.controller;

import cn.valo.system.domain.SystemDictionaryType;
import cn.valo.system.query.SystemDictionaryTypeQuery;
import cn.valo.system.service.ISystemDictionaryDetailService;
import cn.valo.system.service.ISystemDictionaryTypeService;
import cn.valo.basic.util.AjaxResult;
import cn.valo.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systemdt")
public class SystemDictionaryTypeController {
    @Autowired
    private ISystemDictionaryTypeService iSystemDictionaryTypeService;
    @Autowired
    private ISystemDictionaryDetailService iSystemDictionaryDetailService;
    //id查询
    @GetMapping("/{id}")
    public SystemDictionaryType queryById(@PathVariable("id") Long id){
        return iSystemDictionaryTypeService.queryById(id);
    }
    //查所有
    @GetMapping()
    public List<SystemDictionaryType> queryAll(){
        return iSystemDictionaryTypeService.queryAll();
    }
    //高级查询+分页
    @PostMapping
    public PageList<SystemDictionaryType> list(@RequestBody SystemDictionaryTypeQuery query){
        System.out.println("-------------");
        System.out.println(query);
        return iSystemDictionaryTypeService.queryPage(query);
    }

    @DeleteMapping("/{id}") // /systemDictionaryType/1
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
//            iSystemDictionaryDetailService.removeByTypes_id(id);
            iSystemDictionaryTypeService.removeById(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
    @PutMapping //添加或修改
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType) {
        System.out.println(systemDictionaryType);
        try {
            if (systemDictionaryType.getId() != null) {
                iSystemDictionaryTypeService.update(systemDictionaryType);
            } else {
                iSystemDictionaryTypeService.save(systemDictionaryType);
            }

            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }
    //批量删除
    @PatchMapping
    public AjaxResult delete(@RequestBody List<Long> ids) {
        try {
            for(int i=0;i<ids.size();i++){
                iSystemDictionaryDetailService.removeByTypes_id(ids.get(i));
            }
            iSystemDictionaryTypeService.remove(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
